package ds_industry_2025.industry.gy_10.T1

import org.apache.hudi.DataSourceWriteOptions._
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

import java.text.SimpleDateFormat
import java.util.{Calendar, Properties}

// todo 数据抽取部分
object t1_count {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("t1")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .config("spark.sql.legacy.avro.datetimeRebaseModeInWrite","LEGACY")
      .enableHiveSupport()
      .getOrCreate()

    val conn=new Properties()
    conn.setProperty("user","root")
    conn.setProperty("password","123456")
    conn.setProperty("driver","com.mysql.jdbc.Driver")

    val day=Calendar.getInstance()
    day.add(Calendar.DATE,-1)
    val yesterday=new SimpleDateFormat("yyyyMMdd").format(day.getTime)

    //  todo 创建写入ods的方法1 不需要更改字段
    def to_ods(mysql_name:String,ods_name:String,primaryKey:String,precombinefield:String):Unit={
      val ods_path=s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods.db/${ods_name}"
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false",mysql_name,conn)
        .withColumn("etldate",lit(yesterday))
        .write.format("hudi").mode("append")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(),primaryKey)
        .option(PRECOMBINE_FIELD.key(),precombinefield)
        .option(PARTITIONPATH_FIELD.key(),"etldate")
        .option("hoodie.table.name",ods_name)
        .save(ods_path)
      println(s"${ods_name}写入完成")
    }

    //  todo 创建写入ods的方法2 需要剔除字段
    def to_ods02(mysql_name: String, ods_name: String, col:String,primaryKey: String, precombinefield: String): Unit = {
      val ods_path = s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods.db/${ods_name}"
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false", mysql_name, conn)
        .drop(col)
        .withColumn("etldate", lit(yesterday))
        .write.format("hudi").mode("append")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(), primaryKey)
        .option(PRECOMBINE_FIELD.key(), precombinefield)
        .option(PARTITIONPATH_FIELD.key(), "etldate")
        .option("hoodie.table.name", ods_name)
        .save(ods_path)
      println(s"${ods_name}写入完成")
    }

    // todo 写入数据
    to_ods("EnvironmentData","environmentdata","EnvoId","InPutTime")
    to_ods("ChangeRecord","changerecord","ChangeID,ChangeMachineID","ChangeEndTime")
    to_ods("BaseMachine","basemachine","BaseMachineID","MachineAddDate")
    to_ods02("ProduceRecord","producerecord","ProducePrgCode","ProduceRecordID,ProduceMachineID","ProduceCodeEndTime")
    to_ods("MachineData","machinedata","MachineRecordID","MachineRecordDate")


    spark.close()

  }

}
